package cn.xo68.boot.auth.server.mapper;

import cn.xo68.boot.auth.server.domain.OauthToken;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * OauthToken mybatis mapper
 * @author wuyy(16349023@qq.com)
 * @date 2018年08月26日 16:31:14
 *
 */
public interface OauthTokenMapper {


	@Select("SELECT * FROM oauth_token WHERE token = #{token}")
    @Results(value = {
    @Result(id = true, column = "token", property = "token"),
    @Result(column = "user_id", property = "userId"),
    @Result(column = "account", property = "account"),
    @Result(column = "user_name", property = "userName"),
    @Result(column = "client_id", property = "clientId"),
     @Result(column = "client_name", property = "clientName"),
     @Result(column = "create_time", property = "createTime"),
     @Result(column = "expire_time", property = "expireTime"),
     @Result(column = "refresh_token", property = "refreshToken")
    })
    OauthToken get(@Param("token") String token);
	
	@Select("SELECT * FROM oauth_token WHERE token = #{token}")
    @Results(value = { 
    @Result(id = true, column = "token", property = "token"),
        @Result(column = "user_id", property = "userId"),
        @Result(column = "account", property = "account"),
        @Result(column = "user_name", property = "userName"),
        @Result(column = "client_id", property = "clientId"),
        @Result(column = "client_name", property = "clientName"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "expire_time", property = "expireTime"),
     @Result(column = "refresh_token", property = "refreshToken")
    })
    OauthToken find(@Param("token") String token);
	
	@Select("SELECT * FROM oauth_token")
    @Results(value = { 
    @Result(id = true, column = "token", property = "token"),
        @Result(column = "user_id", property = "userId"),
        @Result(column = "account", property = "account"),
        @Result(column = "user_name", property = "userName"),
        @Result(column = "client_id", property = "clientId"),
        @Result(column = "client_name", property = "clientName"),
        @Result(column = "create_time", property = "createTime"),
        @Result(column = "expire_time", property = "expireTime"),
            @Result(column = "refresh_token", property = "refreshToken")
    })
    List<OauthToken> list();
	
	@Insert("insert into oauth_token (token ,user_id ,account ,user_name ,client_id ,client_name ,create_time ,expire_time,refresh_token ) values (#{token} , #{userId} , #{account} , #{userName} , #{clientId} , #{clientName} , #{createTime} , #{expireTime} , #{refreshToken} )")
    @Options(useGeneratedKeys = true, keyProperty = "token")
    void insert(OauthToken oauthToken);
	
	@Update("update oauth_token set  user_id=#{userId}, account=#{account}, user_name=#{userName}, client_id=#{clientId}, client_name=#{clientName}, create_time=#{createTime}, expire_time=#{expireTime} where token = #{token}")
    void update(OauthToken oauthToken);

	@Delete("delete FROM oauth_token WHERE token = #{token}")
	void delete(@Param("token") String token);
}